<template>
    <div class="type-box">
        <div class="left-box" ref="leftScroll">
            <ul>
                <li v-for="item in menus" :key="item.miniWallkey" :class="{active:fcid==item.miniWallkey }" @click="reloadData(item.maitKey,item.miniWallkey)">
                    <span>{{ item.title }}</span>
                </li>
            </ul>
        </div>
        <div class="right-box" ref="rightScroll">
            <div>
                <TypeList :items="types" :rowNum="3"></TypeList>
                <div class="sort-bar">
                    <div class="item" v-for="(item, i) in sortList" :key="'typesort'+i" :class="{ active:sort==item.sortKey }" @click="reloadProduct(item.sortKey)">
                        <span>{{ item.title }}</span>
                    </div>
                </div>
                <ProductList :items="products" :singer="true"></ProductList>
            </div>
        </div>
    </div>
</template>
<script>
import BScroll from "better-scroll";
import { mapState, mapActions, mapMutations } from "vuex";

import TypeList from "../components/TypeList";
import ProductList from "../components/ProductList";

export default {
    components: {
        TypeList,
        ProductList
    },
    data() {
        return {
            sort: "pop"
        };
    },
    computed: {
        ...mapState("type", ["menus", "types", "products", "fcid"]),
        ...mapState(["sortList"])
    },
    methods: {
        ...mapMutations("type", ["setfcid"]),
        ...mapActions("type", [
            "loadTypeMenus",
            "loadTypeList",
            "loadTypeProducts"
        ]),
        initScroll() {
            new BScroll(this.$refs.leftScroll, {
                click: true
            });
            this.$rightScroll = new BScroll(this.$refs.rightScroll, {
                click: true
            });
        },
        initScroll() {
            if (this.$lftScroll||this.$rightScroll) {
                this.$lftScroll.refresh();
                this.$rightScroll.refresh();
            } else {
                // 因为BS本身问题，直接在mounted构建无法读取DOM元素
                // 建议在 构建对象时 使用 40ms 延迟
                setTimeout(() => {
                    this.$lftScroll = new BScroll(this.$refs.leftScroll, {
                        click: true
                    });
                    this.$rightScroll = new BScroll(this.$refs.rightScroll, {
                        click: true
                    });
                }, 40);
            }
        },
        reloadData(pid, fcid) {
            if (fcid != this.fcid) {
                this.$rightScroll.scrollTo(0, 0, 500);
                this.sort = "pop";
                this.loadTypeList({ pid });
                this.setfcid(fcid);
                this.loadTypeProducts({ fcid, sort: "pop" });
            }
        },
        reloadProduct(sort) {
            if (sort != this.sort) {
                this.sort = sort;
                this.loadTypeProducts({ fcid: this.fcid, sort });
            }
        }
    },
    activated(){
        // keep-alive 当前组件从缓存激活时执行的方法
        this.initScroll(); // 解决vue组件缓存导致的better-scroll 失效问题
    },
    mounted() {
        this.initScroll();
        this.loadTypeMenus();
    }
};
</script>
<style lang="less" scoped>
.type-box {
    position: absolute;
    top: @h;
    bottom: @h;
    width: 100%;
    overflow: hidden;
}
.left-box {
    height: 100%;
    display: inline-block;
    width: 30%;
    background-color: @ef;
    vertical-align: top;
    > ul {
        list-style: none;
        padding-left: 0px;
        text-align: center;
        font-size: 34px;
        > li {
            padding: 32px 0px;
            > span {
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                display: block;
                padding: 0px 10px;
                border-left: 10px solid @ef;
                border-right: 10px solid @ef;
                box-sizing: border-box;
            }
        }
        .active {
            background-color: white;
            > span {
                border-left: 10px solid @pink;
            }
        }
    }
}
.right-box {
    height: 100%;
    display: inline-block;
    width: 70%;
    vertical-align: top;
    .sort-bar {
        width: 100%;
        border-top: 1px solid @99;
        border-bottom: 1px solid @99;
        padding: 30px 0px;
        font-size: 32px;
        color: @font;
        display: flex;
        flex-flow: row nowrap;
        margin-bottom: 20px;
        .item {
            width: 100% / 3;
            text-align: center;
            border-right: 1px solid @99;
        }
        .item:last-child {
            border: none;
        }
        .item.active {
            color: @pink;
        }
    }
}
</style>
